package vy.org.app.page.member;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.vy.dal.dao.ViewVyPrintLabelDO;
import org.vy.dal.dao.VyAddressDO;
import org.vy.dal.dao.VyDabbaSubscriptionDO;
import org.vy.dal.dao.VyMagazineSubscriptionDO;
import org.vy.dal.dao.VyMemberDO;

import vy.org.app.common.ActionCtx;
import vy.org.app.common.AddressTypeEnum;
import vy.org.app.page.address.AddressHelper;
import vy.org.app.page.printlabel.group.GroupPrintLabelForm;
import vy.org.app.page.subscription.dabba.DabbaForm;
import vy.org.app.page.subscription.dabba.DabbaHelper;
import vy.org.app.page.subscription.magazine.MagazineForm;
import vy.org.app.page.subscription.magazine.MagazineHelper;

public class MemberHelper {
	
	public static DecimalFormat _2_NUMBER = new DecimalFormat("00");
	public static DecimalFormat _4_NUMBER = new DecimalFormat("1900");
	
	public static MemberForm fillMemberForm(MemberForm form, VyMemberDO dto, List<VyAddressDO> addrs, List<VyMagazineSubscriptionDO> magLst, List<VyDabbaSubscriptionDO> dabLst){
		if(dto != null){
			form.setDobDay(String.valueOf(dto.getDayOfBirth()));
			form.setDobMonth(String.valueOf(dto.getMonthOfBirth()));
			form.setDobYear(String.valueOf(dto.getYearOfBirth()));
			
			form.setUpdeshDay(String.valueOf(dto.getDayOfUpdesh()));
			form.setUpdeshMonth(String.valueOf(dto.getMonthOfUpdesh()));
			form.setUpdeshYear(String.valueOf(dto.getYearOfUpdesh()));
			form.setUpdesh(dto.getIsUpdesh().toString());
			form.setIsUpdestha(dto.getIsUpdestha());

			form.setDeleted(dto.getIsDeleted());
			form.setEmail(dto.getEmail());
			form.setFamilyRefType(dto.getFamilyRefType());
			form.setFamilyVyId(dto.getFamilyVyId());
			form.setFatherHusbandName(dto.getFatherHusbandName());
			form.setFirstName(dto.getFirstName());
			form.setRegionalLangName(dto.getRegionalLangName());
			form.setHobbies(dto.getHobbies());
			form.setIdentity(dto.getIdentityNo());
			form.setIdentityType(dto.getIdentityType());
			form.setLanguagesKnown(dto.getLanguagesKnown());
			form.setLastName(dto.getLastName());
			form.setMaritalStatus(dto.getMaritalStatus());
			form.setMiddleName(dto.getMiddleName());
			form.setNationality(dto.getNationality());
			form.setOfficeAddress(AddressHelper.getAddress(AddressTypeEnum.OFFICE, addrs));
			form.setPermanentAddress(AddressHelper.getAddress(AddressTypeEnum.PERMANENT, addrs));
			form.setPresentAddress(AddressHelper.getAddress(AddressTypeEnum.PRESENT, addrs));
			form.setPanNo(dto.getPanNo());
			if(dto.getPhoneNumber() != null)
				form.setPhoneNumber(dto.getPhoneNumber());
			form.setPlaceOfUpdesh(dto.getPlaceOfUpdesh());
			form.setProfession(dto.getProfession());
			form.setQualification(dto.getQualification());
			form.setRefId(dto.getRefId());
			form.setReligion(dto.getReligion());
			form.setSex(dto.getGender());
			form.setUpdeshta(dto.getUpdestha());
			form.setUpdeshOnEvent(dto.getUpdeshOnEvent());
			form.setVyId(dto.getVyId());
			form.setWhatBoughtYou(dto.getWhatBoughtYou());
			form.setComments(dto.getComments());
			
			if(magLst != null && !magLst.isEmpty()){
				List<MagazineForm> mzForms = new ArrayList<MagazineForm>();
				for(VyMagazineSubscriptionDO mzDO : magLst){
					for(VyAddressDO addrDO : addrs){
						if(mzDO.getAddressId().equals(addrDO.getAddressId().toString()))
							mzForms.add(MagazineHelper.fillMagazineForm(mzDO, addrDO));
					}
				}
				form.setMagazines(mzForms);
				form.setMagazineSubscriptionCnt(mzForms.size());
			}
			if(dabLst != null && !dabLst.isEmpty()){
				List<DabbaForm> daForms = new ArrayList<DabbaForm>();
				for(VyDabbaSubscriptionDO daDO : dabLst){
					for(VyAddressDO addrDO : addrs){
						if(daDO.getAddressId().equals(addrDO.getAddressId()))
						daForms.add(DabbaHelper.fillDabbaForm(daDO, addrDO));
					}
				}
				form.setDabbas(daForms);
				form.setDabbaSubscriptionCnt(daForms.size());
			}
		}
		return form;
	}

	public static String getDate(Integer day, Integer month, Integer year) {
		String date = "";
		if( day != null && day > 0 
				&& month != null && month > 0
				&& year != null && year > 0){
			date = _2_NUMBER.format(day) +"/"+ _2_NUMBER.format(month) + "/" + _4_NUMBER.format(year);
		}
		return date;
	}

	public static VyMemberDO fillMemberDO(MemberForm form){
		VyMemberDO dto = new VyMemberDO();
		if(form != null){
			if(form.getDobDay() != null && !"".equals(form.getDobDay().trim()))
				dto.setDayOfBirth(Integer.parseInt(form.getDobDay()));
			if(form.getDobMonth() != null && !"".equals(form.getDobMonth().trim()))
				dto.setMonthOfBirth(Integer.parseInt(form.getDobMonth()));
			if(form.getDobYear() != null && !"".equals(form.getDobYear().trim()))
				dto.setYearOfBirth(Integer.parseInt(form.getDobYear()));
			
			if(form.getUpdeshDay() != null && !"".equals(form.getUpdeshDay().trim()))
				dto.setDayOfUpdesh(Integer.parseInt(form.getUpdeshDay()));
			if(form.getUpdeshMonth() != null && !"".equals(form.getUpdeshMonth().trim()))
				dto.setMonthOfUpdesh(Integer.parseInt(form.getUpdeshMonth()));
			if(form.getUpdeshYear() != null && !"".equals(form.getUpdeshYear().trim()))
				dto.setYearOfUpdesh(Integer.parseInt(form.getUpdeshYear()));

			dto.setRegionalLangName(form.getRegionalLangName());
			dto.setIsDeleted(form.isDeleted());
			dto.setComments(form.getComments());
			dto.setEmail(form.getEmail());
			dto.setFamilyRefType(form.getFamilyRefType());
			dto.setFamilyVyId(form.getFamilyVyId());
			dto.setFatherHusbandName(form.getFatherHusbandName());
			dto.setFirstName(form.getFirstName());
			dto.setHobbies(form.getHobbies());
			dto.setIdentityNo(form.getIdentity());
			dto.setIdentityType(form.getIdentityType());
			
			if(form.getIsUpdestha())
				dto.setIsUpdestha(form.getIsUpdestha());
			
			dto.setLanguagesKnown(form.getLanguagesKnown());
			dto.setLastName(form.getLastName());
			dto.setMaritalStatus(form.getMaritalStatus());
			dto.setMiddleName(form.getMiddleName());
			dto.setNationality(form.getNationality());
//			dto.setOfficeAddress(AddressUtil.getAddress(AddressTypeEnum.OFFICE, addrs));
//			dto.setPermanentAddress(AddressUtil.getAddress(AddressTypeEnum.PERMANENT, addrs));
//			dto.setPresentAddress(AddressUtil.getAddress(AddressTypeEnum.PRESENT, addrs));
			dto.setPanNo(form.getPanNo());
			if(form.getPhoneNumber() != null)
				dto.setPhoneNumber(form.getPhoneNumber());
			dto.setPlaceOfUpdesh(form.getPlaceOfUpdesh());
			dto.setProfession(form.getProfession());
			dto.setQualification(form.getQualification());
			dto.setRefId(form.getRefId());
			dto.setReligion(form.getReligion());
			dto.setGender(form.getSex());
			dto.setUpdestha(form.getUpdeshta());
			dto.setIsUpdesh(Boolean.parseBoolean(form.getUpdesh()));
			dto.setUpdeshOnEvent(form.getUpdeshOnEvent());
			dto.setVyId(form.getVyId());
			dto.setWhatBoughtYou(form.getWhatBoughtYou());
			dto.setLastUpdatedOn(new Date());
			dto.setLastUpdatedBy(ActionCtx.ctx().getUser().getId());
		}
		return dto;
	}
}
